package com.org.action;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.URLEncoder;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.commons.CommonsMultipartFile;

@Controller
public class UploadAction {
	
	@RequestMapping("/upload")
	public void fileupload(@RequestParam("file") CommonsMultipartFile file,
			HttpServletRequest req) throws IOException {
		String path = req.getSession().getServletContext()
				.getRealPath("/fileupload");
	
		InputStream is = file.getInputStream();
		OutputStream os = new FileOutputStream(new File(path,
				file.getOriginalFilename()));
		int len = 0;
		byte[] buffer = new byte[4000];
		while ((len = is.read(buffer)) != -1)
			os.write(buffer, 0, len);
		os.close();
		is.close();
		
	}

	
	@RequestMapping("/batch")
	public void fileupload(@RequestParam("file") CommonsMultipartFile file[],
			HttpServletRequest req) throws IOException {
		String path = req.getSession().getServletContext()
				.getRealPath("/fileupload");
		for (int i = 0; i < file.length; i++) {
			InputStream is = file[i].getInputStream();
			OutputStream os = new FileOutputStream(new File(path,
					file[i].getOriginalFilename()));
			int len = 0;
			byte[] buffer = new byte[4000];
			while ((len = is.read(buffer)) != -1)
				os.write(buffer, 0, len);
			os.close();
			is.close();
		}
	}
	 /**  
     * 文件下载功能  
     * @param request  
     * @param response  
     * @throws Exception  
     */  
    @SuppressWarnings("resource")
	@RequestMapping("/down")  
    public void down(HttpServletRequest request,HttpServletResponse response) throws Exception{  
        //假如以中文名下载的话  
        String filename =request.getParameter("proposal");
        String fileName = request.getSession().getServletContext().getRealPath("/fileupload")+"/"+filename;  
        //获取输入流  
        InputStream bis = new BufferedInputStream(new FileInputStream(new File(fileName)));  
    
        //转码，免得文件名中文乱码  
        filename = URLEncoder.encode(filename,"UTF-8");  
        //设置文件下载头  
        response.addHeader("Content-Disposition", "attachment;filename=" + filename);    
        //1.设置文件ContentType类型，这样设置，会自动判断下载文件类型    
        response.setContentType("multipart/form-data");   
        BufferedOutputStream out = new BufferedOutputStream(response.getOutputStream());  
        int len = 0;  
        while((len = bis.read()) != -1){  
            out.write(len);  
            out.flush();  
        }  
        out.close();  
    }    
}
